package ui.component
{
	import assets.Asset;
	
	import events.LoadedEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Dictionary;
	
	import flashx.textLayout.formats.TextAlign;
	
	import tool.BitmapTool;
	
	public class EquipIcon extends Sprite
	{
		private var _xmlURL:String = Global.assetPath+"UI/jjzb_scbar.xml";
		private var _xmlLoader:URLLoader;
		private var _xml:XML;
		private var _bitmapURL:String;
		
		private var _iconURL:String;
		
		private var _type:int;
		private var _id:int;
		private var _equip:Boolean;
		private var _installed:Boolean;
		private var _inc:uint;
		
		private var _name:String;
		private var _nameText:TextField;
		
		private var _width:int;
		private var _height:int;
		
		private var bitmap:Bitmap;
		private var incHolder:Bitmap;
		private var equipHolder:Bitmap;
		private var installHolder:Bitmap;
		
		///////////////////////////////////////////////////////////////////

		public function get installed():Boolean
		{
			return _installed;
		}

		public function set installed(value:Boolean):void
		{
			_installed = value;
			if(Asset.resBitmapDatas["inc"]==undefined || Asset.resBitmapDatas["equip"]==undefined || Asset.resBitmapDatas["installed"]==undefined )loadInfoIcon()
			else drawInstallIcon();
		}

		public function get equip():Boolean
		{
			return _equip;
		}
		public function set equip(value:Boolean):void
		{
			_equip = value;
			
			if(Asset.resBitmapDatas["inc"]==undefined || Asset.resBitmapDatas["equip"]==undefined || Asset.resBitmapDatas["installed"]==undefined )loadInfoIcon()
			else drawEquipIcon();
		}
		public function get inc():uint
		{
			return _inc;
		}
		public function set inc(value:uint):void
		{
			_inc = value;
			
			if(Asset.resBitmapDatas["inc"]==undefined || Asset.resBitmapDatas["equip"]==undefined || Asset.resBitmapDatas["installed"]==undefined )loadInfoIcon()
			else drawIncIcon();
		}
		override public function get height():Number
		{
			return _height;
		}
		override public function get width():Number
		{
			return _width;
		}
		override public function get name():String
		{
			return _name;
		}
		override public function set name(value:String):void
		{
			_name = value;
			_nameText.text = value;
		}
		///////////////////////////////////////////////////////////////////
		public function EquipIcon(Type:int,ID:int,Inc:int,Width:uint,Height:uint)
		{
			_type = Type;
			_id = ID;
			_inc = Inc;
			_width = Width;
			_height = Height;
			
			var bmd:BitmapData = new BitmapData(Width,Height,true,0x55000000);
			bitmap = new Bitmap(bmd);
			bitmap.x = -Width*0.5;
			bitmap.y = -Height*0.5;
			addChild(bitmap);
			
			_nameText = new TextField;
			_nameText.width = Width;
			_nameText.textColor = 0xdddddd;
			_nameText.mouseEnabled = false;
			var format:TextFormat = new TextFormat("Microsoft YaHei");
			format.align = TextAlign.CENTER;
			_nameText.defaultTextFormat = format;
			_nameText.x = int(-Width*0.5);
			_nameText.y = int(Height*0.15);
			
			incHolder = new Bitmap;
			
			equipHolder = new Bitmap;
			equipHolder.x = -Width*0.5;
			equipHolder.y = -10;
			
			installHolder = new Bitmap;
			installHolder.x = -Width*0.5;
			installHolder.y = -10;
			
			var preStr:String = "000";
			if (_id>9)preStr = "00";
			if (_id>99)preStr = "0";
			if (_id>999)preStr = "";
			_iconURL = Global.assetPath+"UI/icon/equip/"+String(_type)+preStr+String(_id)+".png";
			
			Asset.bitmapdata.addEventListener(LoadedEvent.BITMAPDATA_COMPLETE,iconLoaded);
			Asset.bitmapdata.load(_iconURL);
		}
		
		private function iconLoaded(e:LoadedEvent):void
		{
			if(e.URL==_iconURL)
			{
				Asset.bitmapdata.removeEventListener(LoadedEvent.BITMAPDATA_COMPLETE,iconLoaded);
				
				var iconData:BitmapData = Asset.resBitmapDatas[_iconURL];
				bitmap.bitmapData.draw(iconData);
				
				if(Asset.resBitmapDatas["inc"]==undefined || Asset.resBitmapDatas["equip"]==undefined || Asset.resBitmapDatas["installed"]==undefined )loadInfoIcon()
				else drawIncIcon();
				
				var nbmd:BitmapData = new BitmapData(width,height*0.4,true,0x88000000);
				var nameBack:Bitmap = new Bitmap(nbmd);
				nameBack.x = -width*0.5;
				nameBack.y = height*0.1;
				nameBack.scaleX = 0.98;
				
				addChild(nameBack);
				addChild(_nameText);
				addChild(incHolder);
			}
		}
		
		private function loadInfoIcon():void
		{
			_xmlLoader = new URLLoader;
			_xmlLoader.addEventListener(Event.COMPLETE,xmlLoaded);
			_xmlLoader.load(new URLRequest(_xmlURL));
		}
		
		private function xmlLoaded(e:Event):void
		{
			_xmlLoader.removeEventListener(Event.COMPLETE,xmlLoaded);
			var loader:URLLoader = e.currentTarget as URLLoader;
			_xml = new XML(loader.data);
			
			_bitmapURL = Global.assetPath+_xml.bitmaps.@ima;
			
			Asset.bitmapdata.addEventListener(LoadedEvent.BITMAPDATA_COMPLETE,bmdLoaded);
			Asset.bitmapdata.load(_bitmapURL);
		}
		
		private function bmdLoaded(e:LoadedEvent):void
		{
			if(e.URL==_bitmapURL)
			{
				Asset.bitmapdata.removeEventListener(LoadedEvent.BITMAPDATA_COMPLETE,bmdLoaded);
				var _bitmapDataTotal:BitmapData = Asset.resBitmapDatas[_bitmapURL];
				var _bitmapDatas:Dictionary = BitmapTool.cutImageUseXML(_bitmapDataTotal,new XML(_xml.bitmaps));
				
				Asset.resBitmapDatas["inc"] = _bitmapDatas["inc"];
				Asset.resBitmapDatas["equip"] = _bitmapDatas["equip"];
				Asset.resBitmapDatas["installed"]=_bitmapDatas["installed"];
				
				equipHolder.bitmapData = _bitmapDatas["equip"];
				installHolder.bitmapData = _bitmapDatas["installed"];
				
				drawIncIcon();
				drawEquipIcon();
				drawInstallIcon();
			}
		}
		
		private function drawIncIcon():void
		{
			var incIcon:BitmapData = Asset.resBitmapDatas["inc"];
			var incData:BitmapData = new BitmapData(incIcon.width*3,incIcon.height,true,0);
			for(var i:int=0;i<_inc;i++)
			{
				var posX:int = i*incIcon.width;
				incData.copyPixels(incIcon,incIcon.rect,new Point(posX,0));
			}
			
			incHolder.bitmapData = incData;
			incHolder.y = 38;
			incHolder.x = -incIcon.width*_inc/2;
		}
		
		private function drawEquipIcon():void
		{
			if(_equip)
			{
				equipHolder.bitmapData = Asset.resBitmapDatas["equip"];
				addChild(equipHolder)
				
			}else
			{
				try{removeChild(equipHolder);}catch(e:Error){} 
			}
		}
		
		private function drawInstallIcon():void
		{
			if(_installed)
			{
				installHolder.bitmapData = Asset.resBitmapDatas["installed"];
				addChild(installHolder);
			}else
			{
				try{removeChild(installHolder);}catch(e:Error){} 
			}
		}
		
		
	}
}